package com.magicmovie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.magicmovie.entity.Dictionary;
import com.magicmovie.mapper.DictionaryMapper;
import com.magicmovie.service.DictionaryService;
import com.magicmovie.utils.QueryFilters;
import com.magicmovie.utils.data.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Autowired
    private DictionaryMapper dictionaryMapper;
    private static Map<String, Object> fildMap = new HashMap<String, Object>() {
        {
            put("keyNo", "key_no");
            put("keyValue", "key_value");
        }
    };
    @Override
    public List<Map<String, Object>> findByKeyNo(String keyNo) {
        return dictionaryMapper.findByKeyNo(keyNo);
    }

    @Override
    public List<Dictionary> queryByParams(Map<String, Object> params) {
        return dictionaryMapper.queryByParams(params);
    }
    @Override
    public PageModel getByFilter(@RequestBody QueryFilters filters) {
        PageHelper.startPage(filters.getPageNo(), filters.getPageSize());
        filters.format(fildMap);
        Page<Map<String, Object>> maps = dictionaryMapper.getByFilter(filters);

        PageModel pageModel = new PageModel<>();
        pageModel.setTotalRecord(Math.toIntExact(maps.getTotal()));
        pageModel.setPageNo(maps.getPageNum());
        pageModel.setPageSize(maps.getPageSize());
        pageModel.setResults(maps.getResult());
        pageModel.setTotalPage(maps.getPages());
        return pageModel;
    }

    @Override
    public void update(@RequestBody Dictionary dictionary) {
        dictionaryMapper.updateByPrimaryKeySelective(dictionary);
    }

    @Override
    public void insert(@RequestBody Dictionary dictionary) {
         dictionaryMapper.insert(dictionary);
    }

    @Override
    public void delete(Integer id) {
        dictionaryMapper.deleteByPrimaryKey(id);
    }
}
